<?php
class Admin extends Admin_Controller 
{
    function __construct() {
        parent::__construct();
    }
    
    public function index() {  
        $this->load->model('analytics/analytics_model'); //echo date('d/m/Y', 1309564800);
        $results = $this->analytics_model->getList('2011-07-01', '2011-07-12');      //print_r($results); die;
        if(!$results) {
            // include the Google Analytics PHP class
            $analytics = $this->load->library('analytics');     //print_r($this->Analytics);
            //require_once APPPATH .'libraries/Analytics.php';
            
            try {
                // create an instance of the GoogleAnalytics class using your own Google {email} and {password}
                //$ga = new Analytics();
                
                // set the date range we want for the report - format is YYYY-MM-DD
                $analytics->setDateRange('2011-07-01','2011-07-12');
             
                // get the report for date and country filtered by Australia, showing pageviews and visits
                $report = $analytics->getReport(
                    array(
                        'dimensions' => urlencode('ga:date'),
                        'metrics' => urlencode('ga:pageviews,ga:visits'),
                        'sort' => 'ga:date'
                        )
                    );   
                
             
                // print out the $report array
                //print_r($report);
             
            } catch (Exception $e) { 
                print 'Error: ' . $e->getMessage(); 
            }
        }
        else {
            $report = array();  
            foreach($results as $value) {
                $report[date('Ymd', $value->create)] = array('ga:visits' => $value->visit, 'ga:pageviews' => $value->pageview);
            }
        }
        
        $result = $this->analytic($report, array('from' => (int)str_replace('-', '', '2011-07-01'), 'to' => (int)str_replace('-', '', '2011-07-12')));

        
        
        $arrVar = array('title_for_layout' => 'Home Admin Page'. $this->title, 'analytics' => $result); 
        
        /*$this->load->helper('fckeditor');
        //Ckeditor's configuration     
        $fckeditor = array(

            //ID of the textarea that will be replaced
            'id'    =>  'content', 
            'path'  => 'js/ckeditor',

            //Optionnal values
            'config' => array(
                'toolbar'   =>  "Full",     //Using the Full toolbar
                'width'     =>  "550px",    //Setting a custom width
                'height'    =>  '100px',    //Setting a custom height

            )
        );
        
        $arrVar['ckeditor'] = display_ckeditor($fckeditor);*/
        $this->template->addJs('highcharts/highcharts.js', 'TOP');
        $this->template->setLayout($arrVar);
    }
    
    protected function analytic($analytics, $arrDate) {
        $arrResult = $data = array();
        for($i = $arrDate['from']; $i <= $arrDate['to']; $i++) {
            if(!$this->checkDay($i)) continue;
            
            // Date 
            $arrResult['date'] .= (isset($arrResult['date'])) ? ", '". date('d/m', strtotime($i)) ."'" : "'". date('d/m', strtotime($i)) ."'";
            // Value
            if(isset($analytics[$i])) {
                $arrResult['visit'] .= (isset($arrResult['visit'])) ? ', '. $analytics[$i]['ga:visits'] : $analytics[$i]['ga:visits'];
                $arrResult['pageviews'] .= (isset($arrResult['pageviews'])) ? ', '. $analytics[$i]['ga:pageviews'] : $analytics[$i]['ga:pageviews'];
                $data[] = '("'. $analytics[$i]['ga:visits'].'", "'. $analytics[$i]['ga:pageviews'] .'", "'. strtotime($i) .'")';
            }
            else {
                $arrResult['visit'] .= (isset($arrResult['visit'])) ? ', '. 0 : 0;
                $arrResult['pageviews'] .= (isset($arrResult['pageviews'])) ? ', '. 0 : 0;
            }
        }
        /*if($data) {     
            $this->load->model('analytics/analytics_model');
            $this->analytics_model->add($data);
        }*/      
        
        return $arrResult;
    }
    
    /**
     * Check date on month
     * 
     * @param Int $time
     * @return Boolean
     */
    private function checkDay($time) {
        $result = true;
        if(substr($time, 4, 2) == "02" && substr($time, 6) > 28) $result = false;
        elseif(in_array(substr($time, 4, 2), array("01", "03", "05", "07", "09", "10", "12")) && substr($time, 6) > 31) $result = false;
        elseif(substr($time, 6) > 30) $result = false;
        elseif(substr($time, 6) == '00') return false;
        return $result; 
    }
}  
?>
